package com.survivor.menu;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import com.survivor.activity.CoordHelper;
import com.survivor.activity.TextureLoader;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.graphics.Bitmap.Config;
import android.graphics.Paint.FontMetrics;
import android.test.IsolatedContext;
import android.util.FloatMath;

public class MenuButton {

	private static final String PLAYSTR = "Play";
	private static final String EXITSTR = "Exit";
	private static final String RANKLISTSTR = "RankList";
	private static final String HELPSTR = "HelpForCtrl";

	private static final String EASYSTR = "Easy";
	private static final String NORMALSTR = "Normal";
	private static final String HARDSTR = "Hard";
	private static final String RETURNSTR = "ReturnMain";

	boolean isClickPlay = false;
	boolean isClickExit = false;
	boolean isClickRank = false;
	boolean isClickHelp = false;
	boolean isClickEasy = false;
	boolean isClickNormal = false;
	boolean isClickHard = false;
	boolean isClickBack = false;

	RectF rectPlay;
	RectF rectExit;
	RectF rectHelp;
	RectF rectRank;
	RectF rectBack;
	RectF rectEasy;
	RectF rectNormal;
	RectF rectHard;

	private List<Bitmap> bmpArray = new ArrayList<Bitmap>();
	private int[] textures = new int[8];

	private CoordHelper ch;

	private static float ratio = 0.05f;
	float textSize = 0;

	private Paint mPaint;
	private Typeface tf;
	private FontMetrics mFontMetrics;
	private int mLineHeight;
	private int mLineGap;

	private float[] playVerCoord = { -.12f, 0, 0, .12f, 0, 0, -.12f, .2f, 0,
			.12f, .2f, 0 };

	private float playTranX = -.38f;
	private float playTranY = -.3f;

	private float[] normalVerCoord = { -.16f, 0, 0, .16f, 0, 0, -.16f, .2f, 0,
			.16f, .2f, 0 };

	private float easyTranY = -.2f;
	private float normalTranY = -.5f;
	private float hardTranY = -.8f;

	private float[] helpVerCoord = { -.24f, 0, 0, .24f, 0, 0, -.24f, .2f, 0,
			.24f, .2f, 0 };
	private float helpTranY = -.6f;
	private float helpTranX = -.5f;

	private float returnTranY = -.85f;

	private float[] texCoord = { 0, 1, 1, 1, 0, 0, 1, 0 };

	private FloatBuffer playVerBuffer, texBuffer, helpVerBuffer,
			normalVerBuffer;

	public MenuButton(Context context, CoordHelper ch) {
		// TODO Auto-generated constructor stub
		this.ch = ch;

		tf = Typeface.createFromAsset(context.getAssets(), "fonts/huakang.ttf");
		this.ch = ch;

		textSize = ratio * ch.w;
		initTextParams();

		bmpArray.add(createBmp(PLAYSTR));
		bmpArray.add(createBmp(EXITSTR));
		bmpArray.add(createBmpLong(HELPSTR));
		bmpArray.add(createBmpLong(RANKLISTSTR));
		bmpArray.add(createBmpLong(RETURNSTR));
		bmpArray.add(createBmp(EASYSTR));
		bmpArray.add(createBmp(NORMALSTR));
		bmpArray.add(createBmp(HARDSTR));

		playVerBuffer = makeFloatBuffer(playVerCoord);
		helpVerBuffer = makeFloatBuffer(helpVerCoord);
		normalVerBuffer = makeFloatBuffer(normalVerCoord);

		texBuffer = makeFloatBuffer(texCoord);

		initRect();
	}

	private FloatBuffer makeFloatBuffer(float[] src) {
		ByteBuffer bb = ByteBuffer.allocateDirect(src.length * 4);
		bb.order(ByteOrder.nativeOrder());

		FloatBuffer fb = bb.asFloatBuffer();
		fb.put(src);
		fb.position(0);

		return fb;
	}

	private void initRect() {
		rectPlay = new RectF(ch.oglX2ScrX(-.5f), ch.oglY2ScrY(-.1f), ch
				.oglX2ScrX(-.26f), ch.oglY2ScrY(-.3f));
		rectExit = new RectF(ch.oglX2ScrX(.26f), ch.oglY2ScrY(-.1f), ch
				.oglX2ScrX(.5f), ch.oglY2ScrY(-.3f));
		rectHelp = new RectF(ch.oglX2ScrX(-.84f), ch.oglY2ScrY(-.3f), ch
				.oglX2ScrX(-.36f), ch.oglY2ScrY(-.6f));
		rectRank = new RectF(ch.oglX2ScrX(.3f), ch.oglY2ScrY(-.3f), ch
				.oglX2ScrX(.85f), ch.oglY2ScrY(-.65f));
		rectBack = new RectF(ch.oglX2ScrX(-.24f), ch.oglY2ScrY(-.65f), ch
				.oglX2ScrX(.24f), ch.oglY2ScrY(-.95f));
		rectEasy = new RectF(ch.oglX2ScrX(-.12f), ch.oglY2ScrY(0), ch
				.oglX2ScrX(.12f), ch.oglY2ScrY(-.2f));
		rectNormal = new RectF(ch.oglX2ScrX(-.16f), ch.oglY2ScrY(-0.3f), ch
				.oglX2ScrX(.16f), ch.oglY2ScrY(-.6f));
		rectHard = new RectF(ch.oglX2ScrX(-.12f), ch.oglY2ScrY(-0.6f), ch
				.oglX2ScrX(.12f), ch.oglY2ScrY(-.8f));
	}

	private void initTextParams() {
		mPaint = new Paint();
		mPaint.setTextSize(textSize);
		mPaint.setFakeBoldText(true);
		mPaint.setTypeface(tf);
		mPaint.setAntiAlias(true);
		mPaint.setColor(Color.WHITE);

		this.mFontMetrics = this.mPaint.getFontMetrics();
		this.mLineHeight = (int) FloatMath.ceil(Math
				.abs(this.mFontMetrics.ascent)
				+ Math.abs(this.mFontMetrics.descent));
		this.mLineGap = (int) (FloatMath.ceil(this.mFontMetrics.leading));
	}

	private Bitmap createBmp(String text) {
		Bitmap bmp = Bitmap.createBitmap(64, 32, Config.ARGB_8888);
		Canvas canvas = new Canvas();
		canvas.drawColor(Color.BLACK);
		if (text.length() > 5)
			mPaint.setTextSize(21);
		else
			mPaint.setTextSize(25f);
		canvas.setBitmap(bmp);
		canvas.drawText(text, 0, 25, mPaint);
		return bmp;
	}

	private Bitmap createBmpLong(String text) {
		Bitmap bmp = Bitmap.createBitmap(128, 32, Config.ARGB_8888);
		Canvas canvas = new Canvas();
		canvas.drawColor(Color.BLACK);
		mPaint.setTextSize(24f);
		canvas.setBitmap(bmp);
		canvas.drawText(text, 0, 25, mPaint);
		return bmp;
	}

	private int getStringWidth(String s) {
		Rect r = new Rect();
		mPaint.getTextBounds(s, 0, s.length(), r);
		return r.width();

	}

	public void drawReturn(GL10 gl) {
		gl.glLoadIdentity();

		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texBuffer);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, helpVerBuffer);

		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[4]);
		if (!isClickBack)
			gl.glColor4f(0f, 0f, 0f, 0.5f);
		else {
			gl.glColor4f(1f, 1f, 1f, 1f);
		}
		gl.glTranslatef(0, returnTranY, 0);
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
	}

	public void drawPlayAndExit(GL10 gl) {
		gl.glLoadIdentity();

		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texBuffer);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, playVerBuffer);

		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
		if (!isClickPlay)
			gl.glColor4f(0f, 0f, 0f, .5f);
		else {
			gl.glColor4f(1f, 1f, 1f, 1f);
		}
		gl.glTranslatef(playTranX, playTranY, 0);
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);

		gl.glLoadIdentity();
		gl.glTranslatef(-playTranX, playTranY, 0);
		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[1]);
		if (!isClickExit)
			gl.glColor4f(0f, 0f, 0f, 0.5f);
		else
			gl.glColor4f(1f, 1f, 1f, 1f);
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
	}

	public void drawHelpAndRank(GL10 gl) {
		gl.glLoadIdentity();

		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texBuffer);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, helpVerBuffer);

		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[2]);
		if (!isClickHelp)
			gl.glColor4f(0f, 0f, 0f, 0.5f);
		else {
			gl.glColor4f(1f, 1f, 1f, 1f);
		}
		gl.glTranslatef(helpTranX, helpTranY, 0);
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);

		gl.glLoadIdentity();

		gl.glTranslatef(-helpTranX, helpTranY, 0);
		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[3]);
		if (!isClickRank)
			gl.glColor4f(0f, 0f, 0f, 0.5f);
		else
			gl.glColor4f(1f, 1f, 1f, 1f);
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
	}

	public void drawSetting(GL10 gl) {
		gl.glLoadIdentity();

		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texBuffer);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, playVerBuffer);
		gl.glTranslatef(0, easyTranY, 0);
		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[5]);
		if (!isClickEasy)
			gl.glColor4f(0f, 0f, 0f, .5f);
		else {
			gl.glColor4f(1f, 1f, 1f, 1f);
		}

		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);

		gl.glLoadIdentity();
		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[7]);
		gl.glTranslatef(0, hardTranY, 0);
		if (!isClickHard)
			gl.glColor4f(0f, 0f, 0f, 0.5f);
		else
			gl.glColor4f(1f, 1f, 1f, 1f);
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);

		gl.glLoadIdentity();
		gl.glTranslatef(0, normalTranY, 0);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, normalVerBuffer);
		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[6]);
		if (!isClickNormal)
			gl.glColor4f(0f, 0f, 0f, 0.5f);
		else
			gl.glColor4f(1f, 1f, 1f, 1f);
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);

	}

	public void loadTextures(GL10 gl) {
		TextureLoader.loadTextures(gl, bmpArray, textures);
	}

}
